package base.ebay;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PermuteTe {

    ArrayList<List<Integer>> ans = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        if (nums == null )
            return new ArrayList();
        permute(nums, new boolean[nums.length], new ArrayList<Integer>());
        return ans;
    }

    public void permute(int[] nums, boolean[] selected, List list) {
        if (list.size() == nums.length) {
            ans.add(new ArrayList(list));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!selected[i]) {
                list.add(nums[i]);
                selected[i] = true;
                permute(nums, selected, list);
                selected[i] = false;
                list.remove(list.size() - 1);
            }
        }
    }


    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 3};

        PermuteTe p = new PermuteTe();

        List<List<Integer>> ans = p.permute(nums);
        for (int i = 0; i < ans.size(); i++) {
            for (int j = 0; j < ans.get(i).size(); j++) {
                System.out.print(ans.get(i).get(j) + " ");
            }
            System.out.println();
        }

    }


}
